Replace duplicate code for loading project manifest
authorJoshua DeSeno <jdeseno@gmail.com>
Thu, 24 Jul 2014 01:54:28 +0000 (10:54 +0900)
committerJoshua DeSeno <jdeseno@gmail.com>
Thu, 24 Jul 2014 05:46:57 +0000 (14:46 +0900)
src/bin/cargo-build.rs
src/bin/cargo-clean.rs
src/bin/cargo-run.rs
src/bin/cargo-test.rs
src/cargo/util/important_paths.rs

index 81c5102123b80b72159f021a845b19c7576c61ce..349a126216c37026469b9104c7f0a71d28f1b6d1 100644 (file)
@@ -17,7 +17,7 @@ use cargo::ops;
 use cargo::ops::CompileOptions;
 use cargo::core::MultiShell;
 use cargo::util::{CliResult, CliError};
-use cargo::util::important_paths::find_project_manifest;
+use cargo::util::important_paths::{find_root_manifest_for_cwd};
 
 #[deriving(PartialEq,Clone,Decodable,Encodable)]
 pub struct Options {
@@ -40,15 +40,7 @@ fn main() {
 fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>> {
     debug!("executing; cmd=cargo-compile; args={}", os::args());
 
-    let root = match options.manifest_path {
-        Some(path) => Path::new(path),
-        None => try!(find_project_manifest(&os::getcwd(), "Cargo.toml")
-                    .map_err(|_| {
-                        CliError::new("Could not find Cargo.toml in this \
-                                       directory or any parent directory",
-                                      102)
-                    }))
-    };
+    let root = try!(find_root_manifest_for_cwd(options.manifest_path));
 
     let env = if options.release {
         "release"
index 4bd1e7484aaf390e66099b0373777cd821e83f0d..dbe597830574679ce43497430658779a57023dc7 100644 (file)
@@ -16,7 +16,7 @@ use cargo::ops;
 use cargo::{execute_main_without_stdin};
 use cargo::core::MultiShell;
 use cargo::util::{CliResult, CliError};
-use cargo::util::important_paths::find_project_manifest;
+use cargo::util::important_paths::{find_root_manifest_for_cwd};
 
 #[deriving(PartialEq,Clone,Decodable,Encodable)]
 pub struct Options {
@@ -32,15 +32,7 @@ fn main() {
 fn execute(options: Options, _shell: &mut MultiShell) -> CliResult<Option<()>> {
     debug!("executing; cmd=cargo-clean; args={}", os::args());
 
-    let root = match options.manifest_path {
-        Some(path) => Path::new(path),
-        None => try!(find_project_manifest(&os::getcwd(), "Cargo.toml")
-                    .map_err(|_| {
-                        CliError::new("Could not find Cargo.toml in this \
-                                       directory or any parent directory",
-                                      102)
-                    }))
-    };
+    let root = try!(find_root_manifest_for_cwd(options.manifest_path));
 
     ops::clean(&root).map(|_| None).map_err(|err| {
       CliError::from_boxed(err, 101)
index 216d783dab7bb6fc5eb65168a36fac0360139322..024b5df2659743fec5b95c0ff62f13f4bdd5c525 100644 (file)
@@ -8,14 +8,13 @@ extern crate serialize;
 #[phase(plugin, link)]
 extern crate hammer;
 
-use std::os;
 use std::io::process::ExitStatus;
 
 use cargo::ops;
 use cargo::{execute_main_without_stdin};
 use cargo::core::{MultiShell};
 use cargo::util::{CliResult, CliError};
-use cargo::util::important_paths::find_project_manifest;
+use cargo::util::important_paths::{find_root_manifest_for_cwd};
 
 #[deriving(PartialEq,Clone,Decodable)]
 struct Options {
@@ -34,15 +33,7 @@ fn main() {
 }
 
 fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>> {
-    let root = match options.manifest_path {
-        Some(path) => Path::new(path),
-        None => try!(find_project_manifest(&os::getcwd(), "Cargo.toml")
-                    .map_err(|_| {
-                        CliError::new("Could not find Cargo.toml in this \
-                                       directory or any parent directory",
-                                      102)
-                    }))
-    };
+    let root = try!(find_root_manifest_for_cwd(options.manifest_path));
 
     let mut compile_opts = ops::CompileOptions {
         update: options.update,
index 1337b57b87a6f83b97273e1f6b5c5252a1029f8b..6dccc9aaa2dc294cdeca4345411e37ca189a6440 100644 (file)
@@ -8,7 +8,6 @@ extern crate serialize;
 #[phase(plugin, link)]
 extern crate hammer;
 
-use std::os;
 use std::io::process::ExitStatus;
 
 use cargo::ops;
@@ -16,7 +15,7 @@ use cargo::{execute_main_without_stdin};
 use cargo::core::{MultiShell};
 use cargo::util;
 use cargo::util::{CliResult, CliError, CargoError};
-use cargo::util::important_paths::find_project_manifest;
+use cargo::util::important_paths::{find_root_manifest_for_cwd};
 
 #[deriving(PartialEq,Clone,Decodable)]
 struct Options {
@@ -35,15 +34,7 @@ fn main() {
 }
 
 fn execute(options: Options, shell: &mut MultiShell) -> CliResult<Option<()>> {
-    let root = match options.manifest_path {
-        Some(path) => Path::new(path),
-        None => try!(find_project_manifest(&os::getcwd(), "Cargo.toml")
-                    .map_err(|_| {
-                        CliError::new("Could not find Cargo.toml in this \
-                                       directory or any parent directory",
-                                      102)
-                    }))
-    };
+    let root = try!(find_root_manifest_for_cwd(options.manifest_path));
 
     let mut compile_opts = ops::CompileOptions {
         update: options.update,
index df7a6bd7a5edc9e17b16f1ed70fdaea0127341d1..614bfd307674baffb15bec7ffbdbfac5c228e181 100644 (file)
@@ -1,4 +1,5 @@
-use util::{CargoResult, human};
+use std::os::{getcwd};
+use util::{CargoResult, CliError, CliResult, human};
 
 /// Iteratively search for `file` in `pwd` and its parents, returning
 /// the path of the directory.
@@ -29,6 +30,18 @@ pub fn find_project_manifest(pwd: &Path, file: &str) -> CargoResult<Path> {
                       file, pwd.display())))
 }
 
+/// Find the root Cargo.toml
+pub fn find_root_manifest_for_cwd(manifest_path: Option<String>) -> CliResult<Path> {
+    match manifest_path {
+        Some(path) => Ok(Path::new(path)),
+        None => match find_project_manifest(&getcwd(), "Cargo.toml") {
+                    Ok(x) => Ok(x),
+                    Err(_) => Err(CliError::new("Could not find Cargo.toml in this \
+                                                 directory or any parent directory", 102))
+                }
+    }
+}
+
 /// Return the path to the `file` in `pwd`, if it exists.
 pub fn find_project_manifest_exact(pwd: &Path, file: &str) -> CargoResult<Path> {
     let manifest = pwd.join(file);